package net.cardgame.orcalecard;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;

import jp.jma.oraclecard.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;



@SuppressWarnings({ "unchecked", "rawtypes" })
public class AdapterInfoApp extends ArrayAdapter {
	private Context context;
	private ArrayList<HashMap<String, String>> list;
	private int idIconNew = R.drawable.new_release;

	public AdapterInfoApp(Context context,
			ArrayList<HashMap<String, String>> list) {
		super(context, R.layout.item_listview_infoapp, list);
		this.context = context;
		this.list = list;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View rowView = inflater.inflate(R.layout.item_listview_infoapp, parent,
				false);
		TextView tvTitle = (TextView) rowView.findViewById(R.id.tv_title);
		TextView tvSummary = (TextView) rowView.findViewById(R.id.tv_summary);
		TextView tvPosted = (TextView) rowView.findViewById(R.id.tv_posted);
		ImageView ivIconNew = (ImageView) rowView.findViewById(R.id.iv_newicon);

		String summary = "	" + list.get(position).get("summary");
		String posted = list.get(position).get("posted");
		String title = "";
		if (checkNew(posted)) {
			ivIconNew.setImageResource(idIconNew);
			title = "		" + list.get(position).get("title");
		} else {
			title = "	" + list.get(position).get("title");
		}

		posted = analysePost(posted);
		tvTitle.setText(title);
		tvSummary.setText(summary);
		tvPosted.setText(posted);

		return rowView;
	}

	// Phan tich string "posted" thanh dang year month day (thu) hour:minute
	public String analysePost(String posted) {
		String result = "";
		String strYear = "";
		String strMonth = "";
		String strDay = "";
		String strHour = "";
		String strMinute = "";

		strYear = posted.substring(0, 4);
		strMonth = posted.substring(4, 6);
		strDay = posted.substring(6, 8);
		strHour = posted.substring(8, 10);
		strMinute = posted.substring(10, 12);

		int year = Integer.parseInt(strYear);
		int month = Integer.parseInt(strMonth);
		int day = Integer.parseInt(strDay);
		String thu = "";

		int index = DetectThu(year, month, day);
		switch (index) {
		case 0:
			thu = "(SAT)";
			break;
		case 1:
			thu = "(SUN)";
			break;
		case 2:
			thu = "(MON)";
			break;
		case 3:
			thu = "(TUE)";
			break;
		case 4:
			thu = "(WED)";
			break;
		case 5:
			thu = "(THU)";
			break;
		case 6:
			thu = "(FRI)";
			break;
		default:
			break;
		}

		result = strYear + "年" + strMonth + "月" + strDay + "日 " + thu + " "
				+ strHour + ":" + strMinute;
		return result;
	}

	// Nhung tin nao < 30 ngay la tin moi ==> return true;
	// Nguoc lai ==> return false;
	public boolean checkNew(String posted) {
		boolean isNew = false;

		String strYear = "";
		String strMonth = "";
		String strDay = "";
		strYear = posted.substring(0, 4);
		strMonth = posted.substring(4, 6);
		strDay = posted.substring(6, 8);
		int posYear = Integer.parseInt(strYear);
		int posMonth = Integer.parseInt(strMonth);
		int posDay = Integer.parseInt(strDay);

		Calendar current = Calendar.getInstance();
		int curYear = current.get(Calendar.YEAR);
		int curMonth = current.get(Calendar.MONTH) + 1;
		int curDay = current.get(Calendar.DATE);

		Calendar posCal = new GregorianCalendar();
		Calendar curCal = new GregorianCalendar();
		posCal.set(posYear, posMonth, posDay);
		curCal.set(curYear, curMonth, curDay);

		int betweenDate = daysBetween(posCal.getTime(), curCal.getTime());

		if (betweenDate > 30)
			isNew = false;
		else
			isNew = true;

		return isNew;
	}

	public int daysBetween(Date d1, Date d2) {
		return (int) ((d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
	}

	// Tim thu may khi biet ngay, thang, nam
	public int DetectThu(int year, int month, int day) {
		final int a[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

		int soNgay = ((year - 1) % 7) * 365 + (year - 1) / 4;
		if (year % 4 == 0)
			a[1] = 29;

		for (int i = 0; i < (month - 1); i++)
			soNgay += a[i];
		soNgay += day;
		int thu = soNgay % 7;

		return thu;
	}
}
